package io.linfeng.common.utils;

import io.linfeng.common.exception.LinfengException;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author linfeng
 * @date 2022/2/21 10:38
 */
public class FileCheckUtil {


    private static final Set<String> ALLOWED_IMAGE_EXTENSIONS = new HashSet<>(Arrays.asList(
            "jpg", "jpeg", "png", "gif", "bmp"
    ));

    public static void checkSize(long maxSize, long size) {
        // 单位 M
        int len = 1024 * 1024;
        if(size > (maxSize * len)){
            throw new LinfengException("上传文件超出规定大小");
        }
    }

    public static boolean checkFileIsImage(String fileName){
        String extension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
        if (ALLOWED_IMAGE_EXTENSIONS.contains(extension)) {
            return true;
        }
        return false;
    }
}
